home *** CD-ROM | disk | FTP | other *** search
/ The Best of MacTutor - S…e Code for Volumes 1 to 5 / The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin / Source Code / #46 (Jul 89) / XCMD Source / filepeek.c < prev    next >
C/C++ Source or Header  |  1989-05-03  |  5KB  |  232 lines

  1. /********************************/
  2. /* File: FilePeek.c                */
  3. /*                                */
  4. /* Given the sector index into    */
  5. /* an existing file, read the     */
  6. /* sector in and "dump" it to    */
  7. /* the screen                    */
  8. /*                                */
  9. /* Paramters:                    */
  10. /* param0 = file reference    num    */
  11. /*         ( file is open    )        */
  12. /* param1 = sector number         */
  13. /*        (1 sector = 512 bytes)    */
  14. /* ----------------------------    */
  15. /* To Build:                    */
  16. /*                                 */
  17. /* (1) Create a project using    */
  18. /* this file as well as the     */
  19. /* XCMD.Glue.c file. (Set         */
  20. /* project type to     XCMD (or    */
  21. /* XFCN) from the Project menu.    */
  22. /*                                */
  23. /* (2) Bring the project up to    */
  24. /* date.                        */
  25. /*                                */
  26. /* (3) Build Code Resource.     */
  27. /*                                */
  28. /* (4) Use ResEdit to copy the     */
  29. /* resource to your stack.        */
  30. /********************************/
  31.  
  32. #include    <MacTypes.h>
  33. #include    <OSUtil.h>
  34. #include    <MemoryMgr.h>
  35. #include    <FileMgr.h>
  36. #include    <ResourceMgr.h>
  37. #include    <pascal.h>
  38. #include    <strings.h>
  39. #include     "HyperXCmd.h"
  40. #include    "HyperUtils.h"
  41.  
  42.  
  43. #define        SECTOR        512                /*** size of input buffer     ***/
  44. #define        NUMROWS        8                /*** across each line        ***/
  45. #define        NUMLINES    32                /*** lines of data             ***/
  46. #define        NUMBYTES    16                /*** #bytes per row of data    ***/
  47. #define        SPACE        0x020            /*** the space character    ***/
  48.  
  49. long     paramtoNum();
  50. void    appendChar();
  51. void     CopyStrToHandle();
  52. char    *CopyAscii();
  53.  
  54. pascal void main( paramPtr )
  55.     XCmdBlockPtr    paramPtr;
  56. {
  57.     short        err;
  58.     short        fref;
  59.     short        lc;                /* line count             */
  60.     short        rc;                /* row count            */
  61.     long        cnt;
  62.     long        blk;            /* sector number         */
  63.     Handle        outData;
  64.     short        *rPtr;            /* per row                */
  65.     char        *cPtr;            /* for ASCII            */
  66.     char        *aPtr;            /* points to asciival    */
  67.     char        *buf;
  68.     char        asciiStr[32];
  69.     char        curntLine[256];    
  70.     Str31        numString;
  71.         
  72.     outData = 0L;
  73.     if( paramPtr->paramCount == 2 ){    /*** expect two parameters ***/
  74.         /*** (1) Get our input parameters             ***/
  75.         fref = (short)paramtoNum( paramPtr, 0 );
  76.         blk     = paramtoNum( paramPtr, 1 );        
  77.  
  78.         /*** (2) Read a buffer of data                ***/
  79.         blk = blk * SECTOR;
  80.         err = SetFPos( fref, fsFromStart, blk );
  81.         if ( !err ){
  82.             cnt = SECTOR;
  83.             
  84.             /*** We need to keep this data locked     ***/
  85.             /*** Since we can;t trust callbacks not    ***/
  86.             /*** to move stuff, we allocate the     ***/
  87.             /*** buffer as non-relocatable.            ***/
  88.             buf = NewPtr( cnt );
  89.             err = FSRead( fref, &cnt, buf );
  90.             
  91.             if ( err != noErr &&  err != eofErr ){
  92.                 paramPtr->returnValue = 0L;
  93.                 return;
  94.             }
  95.         }
  96.     
  97.         /*** result is returned to hypercard as     ***/
  98.         /*** a null terminated string                ***/
  99.         outData = NewHandle( 0L );
  100.  
  101.         /*** (3) Start filling the output buffer    ***/
  102.         *curntLine = '\0';
  103.             
  104.         /*** first the number of bytes read in        ***/
  105.         NumToHex( paramPtr, cnt, 4 , &numString );
  106.  
  107.         appendChar( PtoCstr( (char *)&numString), '\r' );
  108.         CopyStrToHandle( &numString, outData );
  109.  
  110.         /*** point to the input data ***/
  111.         rPtr = (short *)buf;
  112.         
  113.         for( lc = 0;  lc < NUMLINES; ++lc ){
  114.             *curntLine = '\0';
  115.  
  116.             /*** blk is the sector address     ***/
  117.             NumToHex( paramPtr, blk, 6, &numString );
  118.             PtoCstr( (char *)&numString );
  119.             strcat( curntLine, &numString );
  120.             appendChar( curntLine, ':' );
  121.             appendChar( curntLine, SPACE );
  122.             aPtr = asciiStr;
  123.             blk += NUMBYTES;
  124.             
  125.             for( rc = 0; rc < NUMROWS; ++rc ){
  126.             
  127.                 /*** convert eight shorts per row        ***/
  128.                 /*** if we overrun the buffer, draw        ***/
  129.                 /*** whatever data follows it...        ***/
  130.                 NumToHex( paramPtr, (long)*rPtr, 4 , &numString);
  131.                 strcat( curntLine, PtoCstr( (char *)&numString));
  132.                 appendChar( curntLine, ' ' );
  133.                 
  134.                 cPtr = (char *)rPtr;
  135.                 aPtr = CopyAscii( aPtr, *cPtr++ );
  136.                 aPtr = CopyAscii( aPtr, *cPtr++ );
  137.                 rPtr++;
  138.             }
  139.             
  140.             *aPtr = '\0';    /*** terminate the ascii data ***/
  141.             strcat( curntLine, asciiStr );
  142.             appendChar( curntLine, '\r' );
  143.             
  144.             /*** move  line into output buffer            ***/
  145.             CopyStrToHandle( curntLine, outData );
  146.         }
  147.         
  148.         DisposPtr( buf );
  149.     }
  150.     
  151.     cnt = GetHandleSize( outData );
  152.     *(*outData + cnt) = '\0';
  153.     paramPtr->returnValue = outData;
  154. }
  155.  
  156.  
  157.  
  158. long     paramtoNum( paramPtr, i )
  159.     XCmdBlockPtr    paramPtr;
  160.     short            i;
  161. /************************
  162. * Given a handle to an input
  163. * argument in the paramBlk
  164. * return an integer representation
  165. * of the data.
  166. *
  167. ************************/
  168. {
  169.     Str31        theStr;
  170.     
  171.     HLock( paramPtr->params[ i ] );
  172.     ZeroToPas( paramPtr, *(paramPtr->params[ i ]), &theStr );
  173.     HUnlock( paramPtr->params[ i ] );
  174.     return( StrToLong( paramPtr, &theStr ) );
  175. }
  176.  
  177. void     appendChar( theStr, theChar )
  178.     char    *theStr;
  179.     char    theChar;
  180. /************************
  181. * append the character passed
  182. * to the end of the string 
  183. ************************/
  184. {
  185.     long    len = strlen( theStr );
  186.     char    *theEnd;
  187.  
  188.     theEnd         = theStr + len;
  189.     *theEnd++     = theChar;
  190.     *theEnd     = '\0';
  191. }
  192.  
  193.  
  194. char     *CopyAscii( outStr, theChar )
  195.     char    *outStr;
  196.     char    theChar;
  197. /************************
  198. * if the character passed  
  199. * in the input stream is a 
  200. * printing character, append
  201. * it to the output string,
  202. * otherwise, append the '.'
  203. *
  204. * return the update output 
  205. * string.
  206. ************************/
  207. {
  208.     if ( theChar >= SPACE  &&  theChar <= 0x0D8  ) 
  209.         *outStr++ = theChar;
  210.     else
  211.         *outStr++ = '.';
  212.         
  213.     return( outStr );
  214. }
  215.  
  216.     
  217. void CopyStrToHandle( theStr, hand )
  218.     char    *theStr;
  219.     Handle    hand;
  220. /************************
  221. * Copy the input data to the
  222. * output handle.
  223. *
  224. ************************/
  225. {
  226.     long    cnt     = strlen( theStr );
  227.     long    oldSize    = GetHandleSize( hand );
  228.  
  229.     SetHandleSize( hand, oldSize + cnt );
  230.     BlockMove( theStr, *hand + oldSize, cnt );
  231. }
  232.